package com.example.transation.controller;

import com.example.transation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RequestMapping("/user")
public class UserController {
    //手动改开启事务
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;

    @Autowired
    private UserService userService;

    @RequestMapping("/regist")
    public boolean regist(String userName,String password){

        //开启事务
        TransactionStatus transactionStatus=
                dataSourceTransactionManager.getTransaction(transactionDefinition);
        //操作数据库
        userService.registUser(userName,password);
        //提交事务
        dataSourceTransactionManager.commit(transactionStatus);

        //回滚事务
        dataSourceTransactionManager.rollback(transactionStatus);
        return true;
    }
}
